package edu.bbu.commonproject.backend.server.services.thrift;

import java.io.IOException;
import java.net.Socket;
import java.util.logging.Level;

import edu.bbu.commonproject.backend.server.factory.thrift.ThriftSRVFactory;
import edu.bbu.commonproject.backend.server.services.CommunicationSRVServices;
import edu.bbu.commonproject.backend.utils.ServerLogger;

/**
 * The thrift servers services implementation.
 */
public class ThriftCommunicationSRVServices implements CommunicationSRVServices
{
	/**
	 * Starting the Thrift server.
	 */
	public void startServer()
	{
		new Thread(new Runnable()
		{
			public void run()
			{
				ThriftSRVFactory.thriftServer.serve();
			}

		}).start();

		new Thread(new Runnable()
		{
			public void run()
			{
				ThriftSRVFactory.socketServer.server();
			}
			
		}).start();
		
		ServerLogger.log(Level.INFO, "Server started.");
	}

	/**
	 * Stopping the thrift server.
	 */
	public void stopServer()
	{
		ThriftSRVFactory.thriftServer.stop();
		
		try
		{
			ThriftSRVFactory.socketServer.stop();
		}
		catch (IOException e)
		{
			ServerLogger.log(Level.SEVERE, "The thrift server coud not be stopped.");
		}
		
		ServerLogger.log(Level.INFO, "Server stopped.");
	}
}
